// client.js 向服务端发送消息 并通过 stdio 获取响应

import { spawn } from 'child_process'
const serviceProcess = spawn('node', ['server.js'])

serviceProcess.stdout.on('data', data => {
  const str = `${data}\n`
  process.stdout.write(str)
})

const messages = ['生命有意义吗？', '宇宙的尽头是什么？', '再见！']
messages.forEach((msg, idx) => {
  setTimeout(() => {
    console.log('YOU：', msg)
    serviceProcess.stdin.write(msg)
  }, idx * 1000);
})